﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Ccit.Utils;
using Ccit.OA.MsQuestion;
using Newtonsoft.Json;
using System.Collections.Generic;
using Ccit.OA.Utils;
namespace Ccit.OA.Question
{
    public partial class qst_manage_edit : Ccit.Utils.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                _hidsRecid.Value = (null != Request.QueryString["Id"]) ? Request.QueryString["Id"].ToString() : "0";
                _hidRecId.Value = (null != Request.QueryString["Id"]) ? Request.QueryString["Id"].ToString() : "0";
                _hidTabId.Value = (null != Request.QueryString["TABID"]) ? Request.QueryString["TABID"].ToString() : "";
                 InitPage();
            }
 
        }
        private void InitPage()
        {
            if(_hidsRecid.Value!="0")
            {
                QuestionBLL Bll = new QuestionBLL();
                MsQuestionMes msMes = Bll.GetQuestions(_hidsRecid.Value.ToString());
                if(msMes!=null)
                { 
                    _hidPubRange.Value = msMes.pubrange;
                    _hidPUbRangeMarkId.Value = msMes.pubrangemark;
                    _hidRange.Value = msMes.range;
                    _hidRangeMarkId.Value = msMes.rangemark;
                    txtRanage.Text = SysInterface.GetNameStrByIdStr(msMes.range, msMes.rangemark, ",");
                    txtPubRange.Text = SysInterface.GetNameStrByIdStr(msMes.pubrange, msMes.pubrangemark, ",");
                    txtTitle.Text = msMes.title;
                    txtEndtime.Text = msMes.endtime;
                    txtExplain.Text = msMes.explain;
                    radioType.SelectedValue = msMes.type; 
                }
                Bll = null;
                BindTable();
            }
        }
        private void BindTable()
        {
            QuestionBLL Bll = new QuestionBLL();
            DataSet dSet = Bll.GetQuestion(_hidsRecid.Value.ToString(),"2");
            int iLine = 0;
            if(dSet.Tables[0].Rows.Count>0)
            { 
                for (int i = 0; i < dSet.Tables[0].Rows.Count;i++ )
                {
                    DataRow iRow = dSet.Tables[0].Rows[i];
                    if (iRow["Qst_A2_10"].ToString() != "")
                    {
                        CreateTable(iLine, iRow, Bll);
                        iLine++;
                    }
                }
            }
            Bll = null;
        }

        private void CreateTable(int iLine, DataRow iRow, QuestionBLL Bll)
        {
            
            HtmlTableRow oRow = new HtmlTableRow();
            oRow.ID = "row_" + iLine.ToString();
            oRow.Attributes["class"] = "listrow2";

            HtmlTableCell oCellM = new HtmlTableCell();
            oCellM.Align = "center";
            oCellM.Attributes["class"] = "listcellrow";
            oCellM.Attributes["width"] = "30%";
            oCellM.InnerHtml = iRow["Qst_A2_30"].ToString();
            oRow.Cells.Add(oCellM);

            HtmlTableCell oCellX = new HtmlTableCell();
            oCellX.Align = "center";
            oCellX.Attributes["class"] = "listcellrow";
            oCellX.Attributes["width"] = "8%";
            oCellX.InnerHtml = (iRow["Qst_A2_50"].ToString() == "0") ? "单选" : "多选";
            oRow.Cells.Add(oCellX);

            HtmlTableCell oCellS = new HtmlTableCell();
            oCellS.Align = "center";
            oCellS.Attributes["class"] = "listcellrow";
            oCellS.Attributes["width"] = "10%";
            oCellS.InnerHtml = (iRow["Qst_A2_60"].ToString() == "0") ? "不可输入" : "可输入";
            oRow.Cells.Add(oCellS);

            string qId = (iRow["Qst_A2_10"].ToString() != "") ? iRow["Qst_A2_10"].ToString() : "0";
            string iNum = Bll.GetQuestionInfo(qId);
            HtmlTableCell oCellXX = new HtmlTableCell();
            oCellXX.Align = "center";
            oCellXX.Attributes["class"] = "listcellrow";
            oCellXX.Attributes["width"] = "28%";
            oCellXX.InnerHtml = iNum;
            oRow.Cells.Add(oCellXX);

            HtmlTableCell oCellOrder = new HtmlTableCell();
            oCellOrder.Align = "center";
            oCellOrder.Attributes["class"] = "listcellrow";
            oCellOrder.Attributes["width"] = "10%";
            oCellOrder.InnerHtml = "<a href=\"javascript:doMove(" + iRow["Qst_A2_10"].ToString() + "," + iRow["Qst_A2_40"].ToString() + ",'0')\" class='linktext'>";
            oCellOrder.InnerHtml += "<img src='../images/public/moveup.gif'></a>";
            oCellOrder.InnerHtml += "<a href=\"javascript:doMove(" + iRow["Qst_A2_10"].ToString() + "," + iRow["Qst_A2_40"].ToString() + ",'1')\" class='linktext'>";
            oCellOrder.InnerHtml += "<img src='../images/public/movedown.gif'></a>";
            oRow.Cells.Add(oCellOrder);

            HtmlTableCell oCellDel = new HtmlTableCell();
            oCellDel.Align = "center";
            oCellDel.Attributes["class"] = "listcellrow";
            oCellDel.Attributes["width"] = "12%";
            oCellDel.InnerHtml = "<a href=\"javascript:doEdit('" + iRow["Qst_A2_10"].ToString() + "')\" class='linktext'>";
            oCellDel.InnerHtml += "编辑</a>  ";
            oCellDel.InnerHtml += "<a href=\"javascript:doRemoveRow('tbProduct'," + iRow["Qst_A2_10"].ToString() + ",'" + oRow.ID + "')\" class='linktext'>";
            oCellDel.InnerHtml += "删除</a>"; 
            oRow.Cells.Add(oCellDel);

            tbProduct.Rows.Add(oRow);
        }

        protected void lnbQuery_Click(object sender, EventArgs e)
        {
            InitPage();
        }
        #region Ajax Methods 
        [global::System.Web.Services.WebMethod]
        public static string DoSaveMes(string strJson, string cType)
        {
            QuestionBLL Bll = new QuestionBLL();
            MsQuestionMes ms = (MsQuestionMes)JavaScriptConvert.DeserializeObject(strJson, typeof(MsQuestionMes));
            List<string> lstResult = Bll.DoSaveMes(ms);
            var newObj = new { result = lstResult[0], message = lstResult[1], ctrtype = cType };
            string serExpr = JavaScriptConvert.SerializeObject(newObj);
            Bll = null;
            newObj = null;
            return serExpr;
        }
        [global::System.Web.Services.WebMethod]
        public static string DoOperate(string sRecid, string cType)
        {
            QuestionBLL Bll = new QuestionBLL();
            List<string> lstResult = Bll.DoOperateTions(sRecid,"0");
            var newObj = new { result = lstResult[0], message = lstResult[1], ctrtype = cType };
            string serExpr = JavaScriptConvert.SerializeObject(newObj);
            Bll = null;
            newObj = null;
            return serExpr;
        }
        /// <summary>
        /// 上下移动调整显示顺序
        /// </summary>
        /// <param name="sUserId">用户记录ID</param>
        /// <param name="sOrder">调整前的显示顺序</param>
        /// <param name="sMoveType">移动类型0:上移; 1:下移</param>
        /// <returns></returns>
        [global::System.Web.Services.WebMethod]
        public static string ModeSingleStep(string sId, string sOrder, string sMoveType)
        {  
            QuestionBLL Bll = new QuestionBLL();
            string iNum = "";
            if (sMoveType == "0")
                iNum = Bll.MoveUp(sId,sOrder);
            else
                iNum = Bll.MoveDown(sId, sOrder);
            /* 释放业务对象 */
            Bll = null; 
            return iNum;
        }
        #endregion 
    }
}
